/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.baijia.commons.session;


import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.baijia.commons.config.PassportClientConfig;
import com.baijia.commons.constant.Constant;
/**
 * @title InnocentRequestFilter
 * @desc 白名单请求，不过filter chain
 * @author caoliang
 * @date 2015年12月1日
 * @version 1.0
 */
public final class InnocentRequestFilter extends AbstractConfigurationFilter {
    
    private PassportClientConfig passportClientConfig;
    
    @Override
    public void init(final FilterConfig filterConfig) throws ServletException {
        ServletContext context = filterConfig.getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        passportClientConfig = applicationContext.getBean(PassportClientConfig.class);
    }

    @Override
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
        final FilterChain filterChain) throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (request.getParameter(Constant.ARTIFACTPARAMETERNAME) != null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            String path = request.getRequestURI().substring(request.getContextPath().length());
            if (isInnocentUrl(path)) {
                request.getRequestDispatcher(path).forward(request, servletResponse);
            } else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }
    
    
    private boolean isInnocentUrl(String path){
        if(isInnocentPath(path)){
            return true;
        }
        if(isInnocentRegex(path)){
            return true;
        }
        return false;
    }
    
    /**
     * 不需校验的完整url
     * 没有配置url，全部为false
     * 
     * @param path
     * @return
     */
    private boolean isInnocentPath(String path){
        if(CollectionUtils.isEmpty(passportClientConfig.getClientInnocentUrl())){
            return false;
        }
        return passportClientConfig.getClientInnocentUrl().contains(path);
    }
    
    /**
     * 不需校验的url正则
     * 
     * @param path
     * @return
     */
    private boolean isInnocentRegex(String path){
        if(CollectionUtils.isEmpty(passportClientConfig.getClientInnocentRegex())){
            return false;
        }
        for(String regex : passportClientConfig.getClientInnocentRegex()){
            if(path.matches(regex)){
                return true;
            }
        }
        return false;
    }
    
    @Override
    public void destroy() {
        // nothing to do
    }
}
